﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace avemeGame01.SpriteClasses
{
    public class AnimatedSprite
    {
        #region Fields

        // Dictionary of animations using a enum AnimationKey to identify animations
        private Dictionary<AnimationKey, Animation> animations;
        // The current animation to play for this animated sprite
        private AnimationKey currentAnimation;
        // If false the animation is paused
        private bool isAnimating;

        // The texture used for this animated sprite
        private Texture2D texture;
        // The color tint used for the texture
        private Color tintColor;
        // The X, Y position of this animated sprite
        private Vector2 position;
        // The rotation of this animated sprite
        private float rotation;
        // The X, Yscale of this game objects sprite
        private Vector2 scale;

        #endregion

        #region Properties

        /// <summary>
        /// Dictionary of animations using a enum AnimationKey to identify animations.
        /// Use to add new animations to this animated sprite.
        /// </summary>
        public Dictionary<AnimationKey, Animation> Animations
        {
            get { return animations; }
        }

        /// <summary>
        /// The current animation to play for this animated sprite.
        /// </summary>
        public AnimationKey CurrentAnimation
        {
            get { return currentAnimation; }
            set { currentAnimation = value; }
        }

        /// <summary>
        /// true = animation is playing or being updated
        /// false = animation is paused
        /// </summary>
        public bool IsAnimating
        {
            get { return isAnimating; }
            set { isAnimating = value; }
        }

        /// <summary>
        /// The X, Y position of this animated sprite. (Upper left corner of the texture, Vector.Zero = init value)
        /// </summary>
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        /// <summary>
        /// The rotation of this animated sprite. (Based on the upper left corner of the texture, 0 = init value)
        /// </summary>
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        /// <summary>
        /// The X, Y scale of this animated sprite. (1.0 is 100% = init value)
        /// </summary>
        public Vector2 Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        /// <summary>
        /// The color tint of the texture of this animated sprite (Color.White = init value)
        /// </summary>
        public Color TintColor
        {
            get { return tintColor; }
            set { tintColor = value; }
        }

        /// <summary>
        /// The width of the current animation frame.
        /// </summary>
        public int Width
        {
            get { return animations[currentAnimation].FrameWidth; }
        }

        /// <summary>
        /// The height of the current animation frame.
        /// </summary>
        public int Height
        {
            get { return animations[currentAnimation].FrameHeight; }
        }

        /// <summary>
        /// The Center of the current animation frame. i.e. used as rotation origin.
        /// </summary>
        public Vector2 SpriteCenter
        {
            get
            {
                return new Vector2(
                  (float)Width / 2f,
                  (float)Height / 2f);
            }
        }

        /// <summary>
        /// The Center of current animation frame plus the position of the frame.
        /// </summary>
        public Vector2 SpriteCenterPosition
        {
            get { return position + (SpriteCenter * scale); }
        }

        #endregion

        #region Constructor

        public AnimatedSprite(Texture2D texture, Dictionary<AnimationKey, Animation> animations)
        {
            this.texture = texture;
            this.animations = animations;

            // set init values for some fields
            scale = new Vector2(1.0f, 1.0f); // set scale to 100%
            tintColor = Color.White; // set color to white = no tint at all
            position = Vector2.Zero; // set position to upper left corner of the "level"

            isAnimating = true;
        }

        #endregion

        #region Update & Draw Method

        public void Update(GameTime gameTime)
        {
            if (isAnimating)
                animations[currentAnimation].Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture,
                SpriteCenterPosition,
                animations[currentAnimation].CurrentFrameRect,
                tintColor,
                rotation,
                SpriteCenter,
                scale,
                SpriteEffects.None,
                0);
        }

        #endregion
    }
}